<?php
 /**
  *  @package ARXLib
  *  @version 2
  *  @subpackage Inputs
  *
  *  @copyright Ringo-Web Inc. 2004 - 2005
  */
//////////////////////////////////////////////////////////////////////////////////////
// CARXDate
//////////////////////////////////////////////////////////////////////////////////////

    class CARXDate extends CARXInput
    {
        var $m_format; // y - 2000; m - 03; M - march; d - 01
        var $m_yearBegin;
        var $m_yearEnd;

        function CARXDate($label, $dbFieldName, $format)
        {
            parent::CARXInput($label, $dbFieldName);
            $this->m_format = $format;
            $this->m_yearEnd = date("Y")+1;
            $this->m_yearBegin = $this->m_yearEnd - 100;
        }

        function GetDateFromDB(&$year, &$month, &$day)
        {
            $dbDate = $this->GetValue();
            if ($dbDate)
            {
                list($year, $month, $day) = split("-", $this->GetValue());
            }
            else
            {
                list($year, $month, $day) = array(date("Y"), date("m"), date("d"));
            }
        }

        function SetYearsRange($yearBegin, $yearEnd)
        {
            $this->m_yearBegin = $yearBegin;
            $this->m_yearEnd = $yearEnd;
        }

        function GetYearSelect()
        {
            $result = "";
            $ctrlName = $this->GetHTMLCtrlName();
            
            $result .= "<select name={$ctrlName}_year>\n";
            $yBegin = $this->m_yearBegin;
            $yEnd = $this->m_yearEnd;

            $this->GetDateFromDB($year, $month, $day);
            for ($i = $yEnd; $i >= $yBegin; $i--)
            {
                $selected = $year == $i ? " selected" : "";
                $result .= "<option value=$i{$selected}>$i\n";
            }
            $result .= "</select>\n";
            return $result;
        }


        function GetDaySelect()
        {
            $result = "";
            $ctrlName = $this->GetHTMLCtrlName();
            $result .= "<select name={$ctrlName}_day>\n";

            $this->GetDateFromDB($year, $month, $day);
            for ($i = 1; $i <= 31; $i++)
            {
                $selected = $day == $i ? " selected" : "";
                $result .= "<option value=$i{$selected}>$i\n";
            }
            $result .= "</select>\n";
            return $result;
        }

        function GetMonthSelect($fullMonth)
        {
            $result = "";
            $ctrlName = $this->GetHTMLCtrlName();
            $result .= "<select name={$ctrlName}_month>\n";

            $this->GetDateFromDB($year, $month, $day);
            for ($i = 1; $i <= 12; $i++)
            {
                $selected = $month == $i ? " selected" : "";
                $time = mktime(0, 0, 0, $i, 1, 2001);
                //echo "$time ";
                if ($fullMonth)
                {
                    $monthtxt = date("M", $time);
                }
                else
                {
                    $monthtxt = date("m", $time);
                }
                $result .= "<option value=$i{$selected}>$monthtxt\n";
            }
            $result .= "</select>\n";
            return $result;
        }

        function GetHTML()
        {
            $format = $this->m_format;
            for ($i = 0; $i < strlen($format); $i++)
            {
                if ($format[$i] == "y")
                {
                    $result .= $this->GetYearSelect();
                }
                else if ($format[$i] == "m")
                {
                    $result .=  $this->GetMonthSelect(FALSE);
                }
                else if ($format[$i] == "M")
                {
                    $result .=  $this->GetMonthSelect(TRUE);
                }
                else if ($format[$i] == "d")
                {
                    $result .=  $this->GetDaySelect();
                }
                else
                {
                    $result .=  $format[$i];
                }
            }
            return $result;
        }

        function GetFormPostDates(&$month, &$day, &$year)
        {
            $day = 1;
            $month = 1;
            $year = 2000;
            $ctrlName = $this->GetHTMLCtrlName();
            if (isset($_POST["{$ctrlName}_day"]))
            {
                $day = $_POST["{$ctrlName}_day"];
            }
            if (isset($_POST["{$ctrlName}_month"]))
            {
                $month = $_POST["{$ctrlName}_month"];
            }
            if (isset($_POST["{$ctrlName}_year"]))
            {
                $year = $_POST["{$ctrlName}_year"];
            }
        }

        function MoveValueFromPostVars()
        {
            $this->GetFormPostDates($month, $day, $year);
            $this->m_value = "$year-$month-$day";
        }

        function Submit()
        {
            if ($this->m_parentForm && $this->m_parentForm->InSubmitMode())
            {
                $this->MoveValueFromPostVars();
                if ($this->GetDBTable())
                {
                    $table =& $this->GetDBTable();
                    $this->UpdateTableData(TRUE);
                }
            }
        }
    }



?>